import BigNumber from "bignumber.js";

export const encodeInputDataHex = (data:string) => {
    if(data.indexOf("0x")===0)
    {
        return data;
    }
    return "0x"+data;
}
export const encodeInputDataString = (data:string) => {
    let res = "0x"
    for(let i = 0 ; i < data.length ; i++)
    {
        let codeNum = data.charCodeAt(i);
        if(codeNum >= 127)
        {
            res += encodeURI(data[i]).replace(/%/g,"");
        }
        else
        {
            res += codeNum.toString(16);
        }
    }
    return res;
}

export const decodeInputDataHex = (data:string) => {
    try {
        let dataTemp = data.indexOf("0x") === 0 ? data.replace("0x", "") : data;
        if (dataTemp.length % 2) return '';
        let tmp = '';
        for (let i = 0; i < dataTemp.length; i += 2) {
            tmp += '%' + dataTemp.charAt(i) + dataTemp.charAt(i + 1);
        }
        return decodeURI(tmp);
    }catch (e) {
        console.log(e)
        return "";
    }
}

export const checkAddressEq = (addressOne:string,addressTwo:string) => {
    return addressOne.toLowerCase()===addressTwo.toLowerCase();
}
export const getNumberFrom16Or10 = (num:string) => {
    if(num.indexOf("0x")===0)
    {
        return parseInt(num)
    }
    return Number(num)
}

export const BigMul = (num1:number|string|BigNumber,num2:number|string|BigNumber) => {
    return (new BigNumber(num1)).multipliedBy(num2).toFixed();
}
export const BigAdd = (num1:number|string|BigNumber,num2:number|string|BigNumber) => {
    return (new BigNumber(num1)).plus(num2).toFixed();
}
export const BigDiv = (num1:number|string|BigNumber,num2:number|string|BigNumber) => {
    return (new BigNumber(num1)).div(num2).toFixed();
}
export const BigSub = (num1:number|string|BigNumber,num2:number|string|BigNumber) => {
    return (new BigNumber(num1)).minus(num2).toFixed();
}
export const BigPow = (num:number|string|BigNumber,powerNum:number|string|BigNumber) => {
    return (new BigNumber(num)).pow(powerNum).toFixed();
}

